//
//  Region_cityCtl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "Region_cityCtl.h"
#import "CGFdbmBase.h"

#import "CityModel.h"

#import "AreaControl.h"

#import "SelectNavView.h"

@interface Region_cityCtl ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *citys;

@property (nonatomic,strong) UITableView *tableView;
@end

@implementation Region_cityCtl

-(NSMutableArray *)citys{
    if (!_citys) {
        _citys = [NSMutableArray array];
    }
    
    return _citys;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    [self NavView];
}

-(void)NavView{
    SelectNavView *nav = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 64)];
    
    nav.titile.alpha = 0;
    
    nav.share.alpha = 0;
    [nav.bacBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:nav];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *selectString= [NSString stringWithFormat:@"SELECT * FROM region_city WHERE provincecode = %@",self.provincecode];
    
    self.citys =  [CGFdbmBase SearchSelfPathSqlWhithPath:@"diaoyur_city" AndTabName:@"region_city" SelectSting:selectString];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStylePlain];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.dataSource = self;
    self.tableView.delegate  = self;
    
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.citys.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    CityModel *model = [[CityModel alloc]initWithDictionary:self.citys[indexPath.row] error:nil];
    
    cell.textLabel.text = model.name;
    
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.font = [UIFont systemFontOfSize:SCREEN_WIDTH*0.04];
    cell.textLabel.alpha = 0.7;

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CityModel *model = [[CityModel alloc]initWithDictionary:self.citys[indexPath.row] error:nil];
    
    AreaControl *regist = [AreaControl new];
    
    regist.code = model.code;
    
    regist.ares = [NSMutableArray arrayWithObject:self.citys[indexPath.row]];
    
    [self.navigationController pushViewController:regist animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
  return   SCREEN_HEIGHT*0.07;
}

@end
